package com.moshang.blog.controller.back.system;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moshang.blog.core.annotation.SystemLog;
import com.moshang.blog.core.utils.PageUtil;
import com.moshang.blog.entity.Rescource;
import com.moshang.blog.entity.SysDict;
import com.moshang.blog.service.SysDictService;
import com.moshang.blog.service.SysRescourceService;
import com.moshang.blog.core.utils.RestResponse;
import com.moshang.blog.core.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;


/**
 * 
 *
 * @author moshang
 * @email 597575122@qq.com
 * @date 2018-09-21 14:45:09
 */
@Controller
@RequestMapping("/back/system/resource")
public class SysResourcesController {
    @Autowired
    private SysRescourceService sysResourcesService;
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public String list(Model model){
        //查找上传文件来源
        List<SysDict> dictList=sysDictService.findByCondition("上传文件来源");
        model.addAttribute("dictList",dictList);
        List<SysDict> dictList1=sysDictService.findByCondition("文件分类");
        model.addAttribute("dictList1",dictList1);
        return "back/system/resource/list";
    }

    @PostMapping("list")
    @ApiOperation(value = "/list", httpMethod = "POST", notes = "查询系统资源")
    @ResponseBody
    public PageUtil<Rescource> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                    @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                    ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        PageUtil<Rescource> pageUtil = new PageUtil<>();
        QueryWrapper<Rescource> wrapper = new QueryWrapper<>();
        if(!map.isEmpty()){
            String source = (String) map.get("source");
            String type = (String) map.get("type");
            String hash=(String) map.get("hash");
            if(StringUtils.isNotBlank(source)) {
                wrapper.eq("source", source);
            }
            if(StringUtils.isNotBlank(type)) {
                wrapper.eq("type", type);
            }
            if(StringUtils.isNotBlank(hash)) {
                wrapper.eq("hash", hash);
            }
        }
        wrapper.orderByDesc("update_time");
        IPage<Rescource> dataPage = sysResourcesService.page(new Page<>(page,limit),wrapper);
        pageUtil.setCount((int) dataPage.getTotal());
        for (Rescource rescource:dataPage.getRecords()){
            if (rescource.getCategory()!=null) {
                SysDict sysDict = sysDictService.getById(rescource.getCategory());
                rescource.setCategoryName(sysDict.getValue());
            }
        }
        pageUtil.setData(dataPage.getRecords());
        return pageUtil;
    }

    /**
     * 删除系统字典
     */
    @RequiresPermissions("sys:resource:delete")
    @PostMapping("delete")
    @SystemLog("删除系统资源")
    @ApiOperation(value = "/delete", httpMethod = "POST", notes = "编辑系统资源")
    @ResponseBody
    public RestResponse deleteById(@RequestParam("id") Integer id){
        sysResourcesService.removeById(id);
        return RestResponse.success();
    }
    /**
     * 进入添加页面
     * @param model
     * @return
     */
    @GetMapping("addUI")
    public String add(Model model){
      //查找文件分类
        List<SysDict> dictList=sysDictService.findByCondition("文件分类");
        model.addAttribute("dictList",dictList);
        return "back/system/resource/add";
    }
    /**
     * 添加用户操作
     * @return
     */
    //@RequiresPermissions("sys:resource:add")
    @PostMapping("add")
    @ResponseBody
    @SystemLog("添加资源")
    @ApiOperation(value = "/add", httpMethod = "POST", notes = "添加资源")
    public RestResponse add(Rescource rescource){
        QueryWrapper<Rescource> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("web_url",rescource.getWebUrl());
        queryWrapper.eq("file_name",rescource.getFileName());
        Rescource rescource1=sysResourcesService.getOne(queryWrapper);
        if (rescource1!=null){
            rescource1.setCategory(rescource.getCategory());
            sysResourcesService.updateById(rescource1);
            return RestResponse.success("添加成功");
        }else {
            return  RestResponse.failure("添加失败");
        }


    }

}
